$(function () {
    $.when(getdatadictionary("progect_type"), getdatadictionarylike("progect_type_smal")).done($("#jqGrid").jqGrid({
        url: baseURL + 'api/bentrycriteria/list?aguid=' + GetUrlParam("aguid"),
        datatype: "json",
        colModel: [
            {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
            {label: '预算标准ID', name: 'bGuid', index: 'b_guid', width: 80, hidden: true},
            {
                label: '项目类别', name: 'eCType', index: 'e_c_type', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in progect_type) {
                        if (cellvalue == progect_type[d].dKey) return progect_type[d].dValue;
                    }
                }
            },
            {label: '序号', name: 'eCNum', index: 'e_c_num', width: 80},
            {
                label: '项目子类', name: 'eCSaml', index: 'e_c_saml', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in progect_type_smal) {
                        if (cellvalue == progect_type_smal[d].dKey) return progect_type_smal[d].dValue;
                    }
                }
            },
            {label: '准入条件', name: 'eCCriteria', index: 'e_c_criteria', width: 80},
            {label: '所属章节序号', name: 'eCChapter', index: 'e_c_chapter', width: 80},
            {label: '所属章节名称', name: 'eCName', index: 'e_c_name', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    }));
});
var progect_type;
var progect_type_smal;
function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "progect_type") {
            progect_type = r.returnlist;
        }
        vm.progect_type=progect_type;

    });
}
function getdatadictionarylike(sss) {

    $.get(baseURL + "api/bdatadictionary/selectlike?d_code=" + sss, function (r) {
        if (sss == "progect_type_smal") {
            progect_type_smal = r.returnlist;
        }

    });
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            eCType: null,
            eCSaml:null
        },
        showList: true,
        title: null,
        bEntryCriteria: {},
        progect_type: {},
        progect_type_smal_item: {},
        progect_type_smal_item_sech:{}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bEntryCriteria = {};
            vm.bEntryCriteria.bGuid=GetUrlParam("aguid");
            vm.progect_type = progect_type;
            vm.bEntryCriteria.eCType = progect_type[0].dKey;
            var i = 0;
            var item=new Array();
            for (d in progect_type_smal) {
                if (progect_type_smal[d].dCode == "progect_type_smal_" + progect_type[0].dKey) {
                    item[i] = progect_type_smal[d];
                    i++;
                }
            }
            vm.progect_type_smal_item = item;
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bEntryCriteria.guid == null ? "api/bentrycriteria/save" : "api/bentrycriteria/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bEntryCriteria),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bentrycriteria/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "api/bentrycriteria/info/" + guid, function (r) {
                vm.bEntryCriteria = r.bEntryCriteria;
                vm.progect_type = progect_type;
                var i = 0;
                var item=new Array();
                for (d in progect_type_smal) {
                    if (progect_type_smal[d].dCode == "progect_type_smal_" + vm.bEntryCriteria.eCType) {
                        item[i] = progect_type_smal[d];
                        i++;
                    }
                }
                vm.progect_type_smal_item = item;
            });
        },
        onSelectedDrug: function (event) {
            var i = 0;
            var item=new Array();
            for (d in progect_type_smal) {
                if (progect_type_smal[d].dCode == "progect_type_smal_" + event) {
                    item[i] = progect_type_smal[d];
                    i++;
                }
            }
            vm.progect_type_smal_item = item;
        },
        onSelectedshecDrug: function (event) {
            var i = 0;
            var item=new Array();
            for (d in progect_type_smal) {
                if (progect_type_smal[d].dCode == "progect_type_smal_" + event) {
                    item[i] = progect_type_smal[d];
                    i++;
                }
            }
            vm.progect_type_smal_item_sech = item;
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {

                postData:{
                'eCType': vm.q.eCType, 'eCSaml': vm.q.eCSaml
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});